package org.androidpush.utils;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;

import org.androidpush.client.Constants;
import org.androidpush.greendao.ChatMsgDao;
import org.androidpush.greendao.DaoMaster;
import org.androidpush.greendao.DaoSession;
import org.androidpush.greendao.NotificationMsgDao;
import org.androidpush.greendao.ScheduleDao;
import org.androidpush.greendao.SelectCouInfoDao;
import org.androidpush.greendao.StudentDao;
import org.androidpush.greendao.UserFriendDao;

/**
 * Created by huang on 2017/4/18 0018.
 */
public class DBUtils {

    private static DaoSession  session;
    private static SQLiteDatabase db;

    public static ScheduleDao getScheduleDao(Activity activity){
        openDB(activity);
        return session.getScheduleDao();
    }

    public static UserFriendDao getUserFriendDao(Activity activity){
        openDB(activity);
        return session.getUserFriendDao();
    }

    public static ChatMsgDao getChatMsgDao(Activity activity){
        openDB(activity);
        return session.getChatMsgDao();
    }

    public static StudentDao getStudentDao(Activity activity){
        openDB(activity);
        return session.getStudentDao();
    }

    public static NotificationMsgDao getNotificationMsgDao(Activity activity){
        openDB(activity);
        return session.getNotificationMsgDao();
    }

    public static SelectCouInfoDao getSelectCouInfoDao(Activity activity){
        openDB(activity);
        return session.getSelectCouInfoDao();
    }

    private static void openDB(Activity activity){
        db = new DaoMaster.DevOpenHelper(activity, Constants.SHARED_SQLITEDB_NAME,null).getWritableDatabase();
        DaoMaster master = new DaoMaster(db);
        session = master.newSession();
    }

    public static void closeDB(){
        db.close();
    };
}
